home *** CD-ROM | disk | FTP | other *** search
- class CPhitPiece extends CMovieClipFresh
- {
- var _location;
- var _myField;
- var _minTileExtent;
- var _dragPoint;
- var onPress;
- var onRelease;
- var onReleaseOutside;
- var _maxTileExtent;
- var mc_borderHost;
- var _nTiles = 0;
- var _isLerping = false;
- var _lastSteadyLocation = null;
- var _priorLocationTile = null;
- var _lastPushIndex = -1;
- var _lastPushCheckIndex = -1;
- var _lastPushCheckResult = undefined;
- function CPhitPiece()
- {
- super();
- this._location = new Vector2D(0,0);
- this.SetInteractive(true);
- }
- function get _tileWidth()
- {
- return this._myField._tileWidth;
- }
- function get _myField()
- {
- var _loc2_ = CPlayingField(this._parent._parent._parent);
- FreshDebug.Assert(_loc2_ != undefined,"field != undefined");
- return _loc2_;
- }
- function get _tilePosition()
- {
- return this._minTileExtent.GetCopy();
- }
- function get _borderThickness()
- {
- return 3;
- }
- function StartLerping()
- {
- this._isLerping = true;
- this._location._x = this._x;
- this._location._y = this._y;
- }
- function onEnterFrame()
- {
- super.onEnterFrame();
- if(this._isLerping)
- {
- var _loc15_ = 0.5;
- var _loc9_ = this._minTileExtent.GetCopy();
- if(this._dragPoint)
- {
- var _loc14_ = new Vector2D(this._parent._xmouse,this._parent._ymouse);
- var _loc16_ = _loc14_.GetSubtract(this._dragPoint);
- _loc9_.Set(_loc16_);
- _loc9_.DivideScalar(this._tileWidth);
- _loc9_._x = Math.round(_loc9_._x);
- _loc9_._y = Math.round(_loc9_._y);
- var _loc13_ = CPlayingField(this._myField);
- _loc9_._x = MathUtil.Clamp(_loc9_._x,0,_loc13_._trayWidth - 1);
- _loc9_._y = MathUtil.Clamp(_loc9_._y,_loc13_._minTilePos,_loc13_._trayHeight - 1);
- var _loc11_ = _loc9_.GetSubtract(this._minTileExtent);
- var _loc3_ = _loc11_.GetCopy();
- _loc3_.SnapToMajorAxis();
- _loc3_.Normalize();
- if(_loc3_.MagnitudeSquared() > 0.0001)
- {
- var _loc12_ = 2;
- while(true)
- {
- _loc12_;
- if(!_loc12_--)
- {
- break;
- }
- var _loc5_ = this._minTileExtent.GetMultiplyScalar(this._tileWidth);
- var _loc4_ = _loc5_.GetSubtract(this._location);
- var _loc6_ = _loc4_.MagnitudeSafe();
- var _loc8_ = Math.abs(_loc4_.DotProduct(_loc3_));
- if(_loc6_ < 2 || _loc8_ > 0.01)
- {
- var _loc7_ = this.AttemptMoveByDeltaWithPush(_loc3_);
- if(_loc7_)
- {
- break;
- }
- _loc3_ = _loc11_.GetCopy();
- _loc3_.SnapToMinorAxis();
- _loc3_.Normalize();
- }
- }
- }
- }
- _loc9_ = this._minTileExtent.GetCopy();
- _loc9_.MultiplyScalar(this._tileWidth);
- this._location.Set(MathUtil.VectorLerp(_loc9_,this._location,_loc15_));
- var _loc10_ = this._location.GetCopy();
- _loc10_.DivideScalar(this._tileWidth);
- _loc10_._x = Math.round(_loc10_._x);
- _loc10_._y = Math.round(_loc10_._y);
- if(this._priorLocationTile == null)
- {
- this._priorLocationTile = _loc10_.GetCopy();
- }
- if(Math.abs(_loc10_._x - this._priorLocationTile._x) > 0.1 || Math.abs(_loc10_._y - this._priorLocationTile._y) > 0.1)
- {
- }
- this._x = this._location._x;
- this._y = this._location._y;
- this._priorLocationTile.Set(_loc10_);
- }
- }
- function SetInteractive(isInteractive)
- {
- if(isInteractive)
- {
- this.onPress = this.OnMousePress;
- this.onRelease = this.OnMouseRelease;
- this.onReleaseOutside = this.OnMouseRelease;
- this.useHandCursor = true;
- }
- else
- {
- this._dragPoint = null;
- this.onPress = null;
- this.onRelease = null;
- this.onReleaseOutside = null;
- this.useHandCursor = false;
- }
- }
- function OnMousePress()
- {
- this._dragPoint = new Vector2D(this._xmouse,this._ymouse);
- this.StartLerping();
- }
- function OnMouseRelease()
- {
- this._dragPoint = null;
- if(!this._lastSteadyLocation.IsEqual(this._minTileExtent))
- {
- this._myField.RecordTravellingPieceMovement();
- }
- }
- function RecordLastMove()
- {
- this._myField.RecordPieceMove(this,this._lastSteadyLocation.GetCopy(),this._minTileExtent.GetCopy());
- this._lastSteadyLocation = this._minTileExtent.GetCopy();
- }
- function Create(r, g, b, arrTiles)
- {
- var _loc6_ = CPlayingField(this._myField);
- this._minTileExtent = new Vector2D(100000,100000);
- this._maxTileExtent = new Vector2D(-100000,-100000);
- var _loc3_ = 0;
- while(_loc3_ < arrTiles.length)
- {
- FreshDebug.Assert(arrTiles[_loc3_] != undefined,"arrTiles[ i ] != undefined");
- this._minTileExtent._x = Math.min(this._minTileExtent._x,arrTiles[_loc3_]._x);
- this._minTileExtent._y = Math.min(this._minTileExtent._y,arrTiles[_loc3_]._y);
- this._maxTileExtent._x = Math.max(this._maxTileExtent._x,arrTiles[_loc3_]._x);
- this._maxTileExtent._y = Math.max(this._maxTileExtent._y,arrTiles[_loc3_]._y);
- _loc6_.SetTileOwner(arrTiles[_loc3_],this);
- _loc3_ = _loc3_ + 1;
- }
- _loc3_ = 0;
- while(_loc3_ < arrTiles.length)
- {
- var _loc4_ = new Vector2D((arrTiles[_loc3_]._x - this._minTileExtent._x) * this._tileWidth,(arrTiles[_loc3_]._y - this._minTileExtent._y) * this._tileWidth);
- if(_loc4_ == undefined)
- {
- FreshDebug.Trace("Faulty tile location " + _loc3_);
- FreshDebug.Trace(_loc4_.GetStringForm());
- FreshDebug.Trace(this._minTileExtent.GetStringForm());
- FreshDebug.Trace(this._tileWidth);
- }
- var _loc5_ = CPhitTile(this.attachMovie("Tile","tile" + _loc3_,this.getNextHighestDepth(),_loc4_));
- _loc5_.Initialize(this,r,g,b);
- _loc3_ = _loc3_ + 1;
- }
- this._nTiles = arrTiles.length;
- this._x = this._minTileExtent._x * this._tileWidth;
- this._y = this._minTileExtent._y * this._tileWidth;
- this.RenderOutline(r << 16 | g << 8 | b);
- }
- function GetRandomCardinalDelta(upBias)
- {
- if(upBias != undefined)
- {
- if(upBias > _root.random.GetRandom())
- {
- return new Vector2D(0,-1);
- }
- }
- var _loc2_ = new Vector2D(1,0);
- if(_root.random.GetBoolean())
- {
- _loc2_._x = 0;
- _loc2_._y = 1;
- }
- if(_root.random.GetBoolean())
- {
- _loc2_.Invert();
- }
- return _loc2_;
- }
- function SlideRandomly(minDesiredSteps, maxSteps, upBias)
- {
- if(minDesiredSteps == undefined)
- {
- minDesiredSteps = 1;
- }
- var _loc6_ = _root.random.GetIntInRange(minDesiredSteps,maxSteps);
- var _loc5_ = this.GetRandomCardinalDelta(upBias);
- var _loc3_ = 0;
- while(_loc3_ < _loc6_)
- {
- var _loc4_ = this.AttemptMoveByDelta(_loc5_);
- if(!_loc4_)
- {
- break;
- }
- _loc3_ = _loc3_ + 1;
- }
- }
- function IsCompletelyOutsideOfTray()
- {
- var _loc4_ = CPlayingField(this._myField);
- var _loc2_ = 0;
- while(_loc2_ < this._nTiles)
- {
- var _loc3_ = this["tile" + _loc2_];
- if(_loc4_.IsInTray(_loc3_._tilePosition))
- {
- return false;
- }
- _loc2_ = _loc2_ + 1;
- }
- return true;
- }
- function IsCompletelyInsideOfTray()
- {
- var _loc4_ = CPlayingField(this._myField);
- var _loc2_ = 0;
- while(_loc2_ < this._nTiles)
- {
- var _loc3_ = this["tile" + _loc2_];
- if(!_loc4_.IsInTray(_loc3_._tilePosition))
- {
- return false;
- }
- _loc2_ = _loc2_ + 1;
- }
- return true;
- }
- function IsValidMoveByDelta(delta)
- {
- var _loc6_ = CPlayingField(this._myField);
- var _loc2_ = 0;
- while(_loc2_ < this._nTiles)
- {
- var _loc5_ = this["tile" + _loc2_];
- var _loc4_ = _loc5_._tilePosition.GetAdd(delta);
- if(!_loc6_.IsInBounds(_loc4_))
- {
- return false;
- }
- var _loc3_ = _loc6_.GetPieceAtTileLocation(_loc4_);
- if(_loc3_ != null && _loc3_ != this)
- {
- return false;
- }
- _loc2_ = _loc2_ + 1;
- }
- return true;
- }
- function IsValidPosition(tilePos)
- {
- return this.IsValidMoveByDelta(tilePos.GetSubtract(this._minTileExtent));
- }
- function AttemptMoveByDelta(delta)
- {
- if(delta.MagnitudeSafe() < 0.00001)
- {
- return true;
- }
- if(this.IsValidMoveByDelta(delta))
- {
- this.MoveByDelta(delta);
- return true;
- }
- return false;
- }
- function AttemptMoveByDeltaWithPush(delta)
- {
- if(delta.MagnitudeSafe() < 0.00001)
- {
- return true;
- }
- if(this.IsValidMoveByDeltaWithPush(delta,new Date().getTime() / 1000))
- {
- this.MoveByDeltaWithPush(delta,new Date().getTime() / 1000);
- return true;
- }
- return false;
- }
- function IsValidMoveByDeltaWithPush(delta, recursionIndex)
- {
- if(recursionIndex == this._lastPushCheckIndex)
- {
- return this._lastPushCheckResult;
- }
- this._lastPushCheckResult = undefined;
- this._lastPushCheckIndex = recursionIndex;
- var _loc7_ = CPlayingField(this._myField);
- var _loc3_ = 0;
- while(_loc3_ < this._nTiles)
- {
- var _loc6_ = this["tile" + _loc3_];
- var _loc5_ = _loc6_._tilePosition.GetAdd(delta);
- if(!_loc7_.IsInBounds(_loc5_))
- {
- this._lastPushCheckResult = false;
- return false;
- }
- var _loc2_ = _loc7_.GetPieceAtTileLocation(_loc5_);
- if(_loc2_ != null && _loc2_ != this)
- {
- var _loc4_ = _loc2_.IsValidMoveByDeltaWithPush(delta,recursionIndex);
- if(_loc4_ != undefined)
- {
- if(_loc4_ == false)
- {
- this._lastPushCheckResult = false;
- return false;
- }
- }
- }
- _loc3_ = _loc3_ + 1;
- }
- this._lastPushCheckResult = true;
- return true;
- }
- function MoveByDeltaWithPush(delta, recursionIndex)
- {
- if(recursionIndex == this._lastPushIndex)
- {
- return undefined;
- }
- this._lastPushIndex = recursionIndex;
- var _loc4_ = CPlayingField(this._myField);
- _loc4_.AddTravellingPiece(this);
- var _loc3_ = 0;
- while(_loc3_ < this._nTiles)
- {
- var _loc5_ = this["tile" + _loc3_];
- var _loc6_ = _loc5_._tilePosition.GetAdd(delta);
- var _loc2_ = _loc4_.GetPieceAtTileLocation(_loc6_);
- if(_loc2_ != null && _loc2_ != this)
- {
- if(this._isLerping)
- {
- _loc2_.StartLerping();
- }
- _loc2_.MoveByDeltaWithPush(delta,recursionIndex);
- }
- if(_loc4_.GetPieceAtTileLocation(_loc5_._tilePosition) == this)
- {
- _loc4_.ClearTileOwner(_loc5_._tilePosition);
- }
- _loc3_ = _loc3_ + 1;
- }
- this._minTileExtent.Add(delta);
- this._maxTileExtent.Add(delta);
- if(!this._isLerping)
- {
- this._x = this._minTileExtent._x * this._tileWidth;
- this._y = this._minTileExtent._y * this._tileWidth;
- }
- _loc3_ = 0;
- while(_loc3_ < this._nTiles)
- {
- _loc5_ = this["tile" + _loc3_];
- _loc4_.SetTileOwner(_loc5_._tilePosition,this);
- _loc3_ = _loc3_ + 1;
- }
- }
- function MoveByDelta(delta)
- {
- var _loc3_ = CPlayingField(this._myField);
- var _loc2_ = 0;
- while(_loc2_ < this._nTiles)
- {
- var _loc4_ = this["tile" + _loc2_];
- if(_loc3_.GetPieceAtTileLocation(_loc4_._tilePosition) == this)
- {
- _loc3_.ClearTileOwner(_loc4_._tilePosition);
- }
- _loc2_ = _loc2_ + 1;
- }
- this._minTileExtent.Add(delta);
- this._maxTileExtent.Add(delta);
- if(!this._isLerping)
- {
- this._x = this._minTileExtent._x * this._tileWidth;
- this._y = this._minTileExtent._y * this._tileWidth;
- }
- _loc2_ = 0;
- while(_loc2_ < this._nTiles)
- {
- _loc4_ = this["tile" + _loc2_];
- _loc3_.SetTileOwner(_loc4_._tilePosition,this);
- _loc2_ = _loc2_ + 1;
- }
- }
- function MoveToPosition(location, recordSteadyMove)
- {
- var _loc2_ = location.GetSubtract(this._minTileExtent);
- this.MoveByDelta(_loc2_);
- if(recordSteadyMove)
- {
- this._lastSteadyLocation = this._minTileExtent.GetCopy();
- }
- }
- function RecordBaseSteadyPosition()
- {
- this._lastSteadyLocation = this._minTileExtent.GetCopy();
- }
- function RenderOutline(color)
- {
- var _loc8_ = CPlayingField(this._myField);
- this.mc_borderHost = this.createEmptyMovieClip("mc_borderHost",this.getNextHighestDepth());
- var _loc5_ = new Array();
- _loc5_.push(new Vector2D(1,0));
- _loc5_.push(new Vector2D(-1,0));
- _loc5_.push(new Vector2D(0,1));
- _loc5_.push(new Vector2D(0,-1));
- var _loc6_ = 0;
- while(_loc6_ < this._nTiles)
- {
- var _loc7_ = this["tile" + _loc6_];
- var _loc4_ = _loc7_._tilePosition;
- var _loc2_ = 0;
- while(_loc2_ < _loc5_.length)
- {
- var _loc3_ = _loc5_[_loc2_];
- if(_loc8_.GetPieceAtTileLocation(_loc4_.GetAdd(_loc3_)) != this)
- {
- this.RenderTileEdge(_loc4_.GetCopy(),_loc3_.GetCopy(),color);
- }
- _loc2_ = _loc2_ + 1;
- }
- _loc6_ = _loc6_ + 1;
- }
- }
- function RenderTileEdge(tilePos, direction, color)
- {
- tilePos.Subtract(this._minTileExtent);
- tilePos.MultiplyScalar(this._tileWidth);
- tilePos.AddScalar(this._tileWidth * 0.5);
- direction.MultiplyScalar(this._tileWidth * 0.5);
- var _loc3_ = direction.GetCopy();
- _loc3_.Transpose();
- var _loc2_ = direction.GetCopy();
- _loc2_.Transpose();
- _loc2_.Invert();
- _loc3_.Add(direction);
- _loc2_.Add(direction);
- _loc3_.Add(tilePos);
- _loc2_.Add(tilePos);
- this.mc_borderHost.lineStyle(this._borderThickness,color);
- this.mc_borderHost.moveTo(_loc3_._x,_loc3_._y);
- this.mc_borderHost.lineTo(_loc2_._x,_loc2_._y);
- }
- }
-